package com.example.springexcel.CopyPojo;

import com.example.springexcel.pojo.Vo.DataWranglingDataVo;
import com.example.springexcel.pojo.dto.DataWranglingData;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
public class CopyDataWrangling {

    /**
     * 将单个Order对象转换为多个OrderVo对象
     * 每个物品生成一个OrderVo记录
     */
    public static List<DataWranglingDataVo> convertToVoList(DataWranglingData order) {
        if (order == null || order.getItemData() == null) {
            return Collections.emptyList();
        }

        return order.getItemData().stream()
                .map(item -> {
                    DataWranglingDataVo vo = new DataWranglingDataVo();

                    // 复制订单基本信息
                    vo.setDate(order.getDate());
                    vo.setFactory(order.getFactory());
                    vo.setRemark(order.getRemark());

                    // 复制物品详情
                    vo.setNameItem(item.getNameItem());
                    vo.setQuantity(item.getQuantity());
                    vo.setUnitPrice(item.getUnitPrice());
//                    log.info("源：{}",order);
//                    log.info("解析到一条数据: {}", vo);
                    return vo;
                })
                .collect(Collectors.toList());
    }

    /**
     * 批量转换多个订单
     */
    public static List<DataWranglingDataVo> convertAllToVoList(List<DataWranglingData> orders) {
        return orders.stream()
                .flatMap(order -> convertToVoList(order).stream())
                .collect(Collectors.toList());
    }

}
